﻿
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ToolUtil
{
    public partial class myButton : System.Windows.Forms.Button
    {
        private Color enterForeColor = Color.White;
        private Color leftForeColor = Color.Black;
        private Color upBackColor = Color.Black;
        private Color dBackColor = Color.Black;
        private bool Isleft = true;
        private int radius=5;//半径 

        public int Radius
        {
            set
            {
                radius = value;
                this.Invalidate();
            }
            get
            {
                return radius;
            }
        }

        public bool IsLEFT
        {
            get { return Isleft; }
            set
            {
                this.Isleft = value;
            }
        }
        public Color UpBackColor
        {
            get { return upBackColor; }
            set
            {
                this.upBackColor = value;
            }
        }
        public Color DBackColor
        {
            get { return dBackColor; }
            set
            {
                this.dBackColor = value;
            }
        }
        public Color EnterForeColor
        {
            get { return enterForeColor; }
            set
            {
                this.enterForeColor = value;
                this.ForeColor = value;
            }
        }
        public Color LeftForeColor
        {
            get { return leftForeColor; }
            set
            {
                this.leftForeColor = value;
                this.ForeColor = value;

            }
        }
        [DefaultValue(typeof(Color), "51, 161, 224")]
        //[DefaultValue(typeof(Color), "220, 80, 80")]


        //  [DefaultValue(typeof(Color), "251, 161, 0")]
        protected override void OnMouseEnter(EventArgs e)//鼠标进入时
        {
            base.OnMouseEnter(e);
            this.ForeColor = this.EnterForeColor;
        }
        protected override void OnMouseLeave(EventArgs e)//鼠标离开
        {
            base.OnMouseLeave(e);
            this.ForeColor = this.LeftForeColor;
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            base.OnPaintBackground(e);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            var path = GetRoundedRectPath(rect, radius);
            this.Region = new Region(path);
            if (this.Enabled)
            {
                //渐变填充
                using (LinearGradientBrush b = new LinearGradientBrush(rect, upBackColor, dBackColor, LinearGradientMode.Vertical))
                {
                    e.Graphics.FillPath(b, path);
                    System.Drawing.Font fo = new System.Drawing.Font(this.Font.Name, this.Font.Size);
                    Brush brush = new SolidBrush(this.ForeColor);
                    StringFormat gs = new StringFormat();
                    gs.Alignment = StringAlignment.Center; //居中
                    gs.LineAlignment = StringAlignment.Center;//垂直居中
                    e.Graphics.DrawString(this.Text, fo, brush, rect, gs);
                }
            }
            else
            {
                using (SolidBrush b = new SolidBrush(Color.Coral))
                {
                    e.Graphics.FillPath(b, path);
                    System.Drawing.Font fo = new System.Drawing.Font(this.Font.Name, this.Font.Size);
                    Brush brush = new SolidBrush(this.ForeColor);
                    StringFormat gs = new StringFormat();
                    gs.Alignment = StringAlignment.Center; //居中
                    gs.LineAlignment = StringAlignment.Center;//垂直居中
                    e.Graphics.DrawString(this.Text, fo, brush, rect, gs);
                }
            }

        }
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new System.Drawing.Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            path.AddArc(arcRect, 180, 90);
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }
    }

}
